﻿using Api.Application.Command.Region;
using Api.DTO;
using AutoMapper;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Region
{
    public class ShowRegionHandler : IRequestHandler<ShowRegionCommand, ApiPaging<RegionDTO>>
    {
        private readonly IBase<RegionModel> regionRepo;
        private readonly IMapper mapper;

        public ShowRegionHandler(IBase<RegionModel> regionRepo, IMapper mapper)
        {
            this.regionRepo = regionRepo;
            this.mapper = mapper;
        }

        public Task<ApiPaging<RegionDTO>> Handle(ShowRegionCommand request, CancellationToken cancellationToken)
        {

            ApiPaging<RegionDTO> paging = new ApiPaging<RegionDTO>();
            var province = regionRepo.GetAll().Where(x => x.CityId == request.CityId);
            var provinces = mapper.Map<List<RegionDTO>>(province);
            paging.Code = ApiEnum.OK;
            paging.Msg = "获取省份成功";
            paging.Data = provinces;
            return Task.FromResult(paging);
        }
    }
}
